/*
 * Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
 * This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
 * http://www.jspbook.com. You may freely use the code both commercially
 * and non-commercially. If you like the code, please pick up a copy of
 * the book and help support the authors, development of more free code,
 * and the JSP/Servlet/J2EE community.
 */
package com.solution.webutil;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.solution.constant.LoginConstants;
import com.solution.exception.RepositoryNotInitializedException;
import com.solution.util.Repository;

public class GZIPFilter implements Filter {
	private Logger log = Logger.getLogger(GZIPFilter.class.getName());

  public void doFilter(ServletRequest req, ServletResponse res,
      FilterChain chain) throws IOException, ServletException {
    if (req instanceof HttpServletRequest) {
      HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) res;
      String requestedURI = request.getRequestURI();
      request.getQueryString();
      String requestMapping = "";
      String requestedAction = "";
      String queryString = request.getQueryString();
      if(requestedURI.lastIndexOf(".jsp") == -1 )
      {
        requestMapping= requestedURI.substring(requestedURI.lastIndexOf("/")+1,requestedURI.lastIndexOf(".do"));
        requestedAction= requestedURI.substring(requestedURI.lastIndexOf("/")+1);
        if(queryString != null)
        {
        	requestedAction +="?"+queryString;
        }
      }
      HttpSession session = request.getSession(false);
      HashMap hashMap = null;
      try {
		String checkActionPermission = Repository.getRepository().getSystemProperty("checkActionPermission");
		if("yes".equalsIgnoreCase(checkActionPermission))
		{
	      if(session != null)
	      {
	    	  if(!"logout.do".equalsIgnoreCase(requestedAction) && ! "changePasswordInit.do?changemode=change".equalsIgnoreCase(requestedAction))
	    	  {
	    	  hashMap = (HashMap)session.getAttribute(LoginConstants.SESSION_USER_ACTION_INFO);
	    	  if(hashMap != null && !"unauthenticatedRequest.do".equalsIgnoreCase(requestedAction) && hashMap.get(requestedAction)==null)
	    	  {
	    		  response.sendRedirect("/unauthenticatedRequest.do");
	    		  return;
	    	  }
	    	  }
	      }
		}
      } catch (RepositoryNotInitializedException e) {
    	  log.error("Repository Not initialized", e);
  	}
      boolean isInvalidforZip = true;
      if( "somePathToBeIgnored".equalsIgnoreCase(requestMapping) || "fileUpload".equalsIgnoreCase(requestMapping))
      	isInvalidforZip = false;

      String ae = request.getHeader("accept-encoding");
      if (ae != null && ae.indexOf("gzip") != -1 && isInvalidforZip ) {
        System.out.println("GZIP supported, compressing.");
        GZIPResponseWrapper wrappedResponse =
          new GZIPResponseWrapper(response);
        chain.doFilter(req, wrappedResponse);
        wrappedResponse.finishResponse();
        return;
      }
      chain.doFilter(req, res);
    }
  }

  public void init(FilterConfig filterConfig) {
    // noop
  }

  public void destroy() {
    // noop
  }
}
